//
//  OMTMemoryCache.m
//  Pods
//
//  Created by yizhaorong on 11/05/2017.
//
//

#import "OMTMemoryCache.h"

#import "OMTImageCache.h"
#import "OMTDataCache.h"

NSString *const OMTMemoryCacheCurrentBundlePath = @"OMTMemoryCacheCurrentBundlePath";

@interface OMTMemoryCache ()

@property (nonatomic, strong) OMTImageCache *imageCache;

@property (nonatomic, strong) OMTDataCache *dataCache;

@property (nonatomic, strong) NSMutableDictionary *dictionary;

@end

@implementation OMTMemoryCache

+ (instancetype)sharedInstance {
    static OMTMemoryCache *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [OMTMemoryCache new];
    });
    return instance;
}

- (instancetype)init {
    if (self = [super init]) {
        _imageCache = [OMTImageCache sharedInstance];
        _dataCache = [OMTDataCache sharedInstance];
        _dictionary = [NSMutableDictionary dictionary];
    }
    return self;
}

+ (void)storeImage:(UIImage *)image key:(NSString *)key {
    if ([image isKindOfClass:[UIImage class]]) {
        OMTMemoryCache *cache = [OMTMemoryCache sharedInstance];
        [cache.imageCache storeImage:image forKey:key];
    }
}

+ (UIImage *)imageForKey:(NSString *)key {
    OMTMemoryCache *cache = [OMTMemoryCache sharedInstance];
    return [cache.imageCache imageForKey:key];
}

+ (void)storeObject:(id)object key:(NSString *)key {
    OMTMemoryCache *cache = [OMTMemoryCache sharedInstance];
    [cache.dataCache storeData:object key:key];
}

+ (id)objectForKey:(NSString *)key {
    OMTMemoryCache *cache = [OMTMemoryCache sharedInstance];
    return [cache.dataCache dataForKey:key];
}

+ (void)setMemoryValue:(id)value key:(NSString *)key {
    OMTMemoryCache *cache = [OMTMemoryCache sharedInstance];
    cache.dictionary[key] = value;
}

+ (id)memoryValueForKey:(NSString *)key {
    OMTMemoryCache *cache = [OMTMemoryCache sharedInstance];
    return cache.dictionary[key];
}

+ (void)clearCache {
    OMTMemoryCache *cache = [OMTMemoryCache sharedInstance];
    [cache.imageCache clearMemory];
    [cache.dataCache clearMemory];
}

+ (void)clearMemoryValue {
    OMTMemoryCache *cache = [OMTMemoryCache sharedInstance];
    [cache.dictionary removeAllObjects];
}

+ (void)clearAll {
    [self clearCache];
    [self clearMemoryValue];
}

@end
